/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2024-2024. All rights reserved.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <cstdint>
#include <hilog/log.h>
#include <string>
#include "napi/n_func_arg.h"
#include "plugin_manager.h"

napi_value TesFunc(napi_env env, napi_callback_info info)
{
    LOGI("enter TesFunc");
    if ((nullptr == env) || (nullptr == info)) {
        LOGE("Play: env or info is null");
        return nullptr;
    }
    
    auto alphaRect = NVal::CreateObject(env);
    auto x = NVal::CreateInt64(env, 10);
    auto y = NVal::CreateInt64(env, 10);
    auto w = NVal::CreateInt64(env, 500);
    auto h = NVal::CreateInt64(env, 40);
    alphaRect.AddProp("x", x.val_);
    alphaRect.AddProp("y", y.val_);
    alphaRect.AddProp("w", w.val_);
    alphaRect.AddProp("h", h.val_);
    
    auto rgbRect = NVal::CreateObject(env);
    rgbRect.AddProp("x", x.val_);
    rgbRect.AddProp("y", y.val_);
    rgbRect.AddProp("w", w.val_);
    rgbRect.AddProp("h", h.val_);
    
    auto version = NVal::CreateInt64(env, 2);
    auto totalFrames = NVal::CreateInt64(env, 300);
    auto width = NVal::CreateInt64(env, 200);
    auto height = NVal::CreateInt64(env, 100);
    auto videoWidth = NVal::CreateInt64(env, 100);
    auto videoHeight = NVal::CreateInt64(env, 2);
    auto orien = NVal::CreateInt64(env, 2);
    auto fps = NVal::CreateInt64(env, 30);
    auto isMix = NVal::CreateBool(env, true);
    auto currentFrame = NVal::CreateInt64(env, 0);
    
    auto animConfig = NVal::CreateObject(env);
    animConfig.AddProp("version", version.val_);
    animConfig.AddProp("totalFrames", totalFrames.val_);
    animConfig.AddProp("width", width.val_);
    animConfig.AddProp("height", height.val_);
    animConfig.AddProp("videoWidth", videoWidth.val_);
    animConfig.AddProp("videoHeight", videoHeight.val_);
    animConfig.AddProp("orien", orien.val_);
    animConfig.AddProp("fps", fps.val_);
    animConfig.AddProp("isMix", isMix.val_);
    animConfig.AddProp("alphaPointRect", alphaRect.val_);
    animConfig.AddProp("rgbPointRect", rgbRect.val_);
    animConfig.AddProp("currentFrame", currentFrame.val_);
    
    return animConfig.val_;
}

EXTERN_C_START
static napi_value Init(napi_env env, napi_value exports)
{
    LOGI("Init begins");
    if ((nullptr == env) || (nullptr == exports)) {
        LOGE("env or exports is null");
        return nullptr;
    }

    napi_property_descriptor desc[] = {
        { "getContext", nullptr, PluginManager::GetContext, nullptr, nullptr, nullptr, napi_default, nullptr },
        { "tes", nullptr, TesFunc, nullptr, nullptr, nullptr, napi_default, nullptr }
    };
    if (napi_ok != napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc)) {
        LOGE("napi_define_properties failed");
        return nullptr;
    }

    PluginManager::GetInstance()->Export(env, exports);
    return exports;
}
EXTERN_C_END

static napi_module nativerenderModule = {
    .nm_version = 1,
    .nm_flags = 0,
    .nm_filename = nullptr,
    .nm_register_func = Init,
    .nm_modname = "vap",
    .nm_priv = ((void *)0),
    .reserved = { 0 }
};

extern "C" __attribute__((constructor)) void RegisterModule(void)
{
    napi_module_register(&nativerenderModule);
}